package com.framework.modules.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.sys.SysTemplate;
import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.page.PageUtils;
import com.framework.service.SysTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RequestMapping("sys/template")
@RestController
public class SysTemplateController extends AbstractController {

    @Autowired
    private SysTemplateService sysTemplateService;

    @RequestMapping("list")
    public RespResult list(@RequestParam Map<String,Object> params){
        PageUtils page=sysTemplateService.queryTemplatePage(params);
        return RespResult.ok().put("page",page);
    }

    @RequestMapping("uploadTemplate")
    public RespResult updateFile(@RequestParam("file") MultipartFile file ) throws IOException {
        String prefix="templet";
        String url ="";
        String suffix;
        if(file!=null) {
            suffix= file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            url= OSSFactory.build().uploadSuffix(file.getBytes(), suffix,prefix);
            System.out.println(url);
        }
        return RespResult.ok().put("url",url);
    }

    @RequestMapping("save")
    public RespResult save(@RequestBody SysTemplate template){
        sysTemplateService.save(template);
        return RespResult.ok();
    }


    @RequestMapping("/getTemplatesByDept")
    public RespResult getTemplatesByDept(){
        List<SysTemplate> list = sysTemplateService.selectList(new EntityWrapper<SysTemplate>().eq("dept_id", getDeptId()).eq("type",1));
        return RespResult.ok().put("list",list);
    }



}
